package com.example.g5project.service.impl;

import com.example.g5project.entity.frontEndObject.FigureData;
import com.example.g5project.entity.frontEndObject.FigureData2;
import com.example.g5project.entity.frontEndObject.PeopleNumberObject;
import com.example.g5project.entity.relation.ConsultInfo;
import com.example.g5project.entity.relation.ConsultantMessages;
import com.example.g5project.entity.relation.HelpMessage;
import com.example.g5project.repository.repo.*;
import com.example.g5project.service.AdministratortService;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.assertj.core.util.Preconditions;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;

/**
 * @author Hu Zirui
 * @version 1.0.0
 * @ClassName ConsultantServiceImpl.java
 * @Description TODO
 * @createTime 2022年03月09日 13:18:00
 */
@Service
@Transactional
public class AdministratorServiceImpl implements AdministratortService {
    private final AdministratorRepo administratorRepo;
    private final ConsultInfoRepo consultInfoRepo;
    private final ConsultMessageRepo consultantMessagesRepo;
    private final NewChatRepo newChatRepo;


    public AdministratorServiceImpl(AdministratorRepo administratorRepo, ConsultInfoRepo consultInfoRepo, ConsultMessageRepo consultantMessagesRepo, NewChatRepo newChatRepo) {
        this.administratorRepo = administratorRepo;
        this.consultInfoRepo = consultInfoRepo;
        this.consultantMessagesRepo = consultantMessagesRepo;
        this.newChatRepo = newChatRepo;
    }

    @Override
    public ArrayList<PeopleNumberObject> getInfoByMonth(int year, int month) throws ParseException {
        Preconditions.checkNotNull(year, "月份不能为空");
        Preconditions.checkArgument(month >= 1 && month <= 12, "月份不合理");
        ArrayList<PeopleNumberObject> result = new ArrayList<>();
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.YEAR, year);
        calendar.set(Calendar.MONTH, month);
        calendar.set(Calendar.DATE, 1);
        calendar.roll(Calendar.DATE, -1);
        int maxDate = calendar.get(Calendar.DATE);
        String dateStr;
        Date date;
        String monthStr = month < 10 ? String.valueOf(month) : "0" + month;
        for (int i = 1; i < maxDate; i++) {
            if (i < 10)
                dateStr = year + "-" + month + "-0" + i;
            else
                dateStr = year + "-" + month + "-" + i;
            DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            PeopleNumberObject peopleNumberObject = new PeopleNumberObject(dateStr, administratorRepo.getCurrentDateConsultantNumber(dateStr), administratorRepo.getCurrentDateSupervisorNumber(dateStr));
            result.add(peopleNumberObject);
        }
        return result;
    }

    @Override
    public ArrayList<String> getConsultantInfoByDate(String date) {
        Preconditions.checkNotNull(date, "日期不能为空");
        return administratorRepo.getConsultantInfoByDate(date);
    }

    @Override
    public ArrayList<String> getSupervisorInfoByDate(String date) {
        Preconditions.checkNotNull(date, "日期不能为空");
        return administratorRepo.getSupervisorInfoByDate(date);
    }

    @Override
    public void addConsultantIntoScheduleByName(String name, String date) {
        Preconditions.checkNotNull(date, "日期不能为空");
        Preconditions.checkNotNull(name, "姓名不能为空");
        Integer id = administratorRepo.getConsultantIDByName(name);
        administratorRepo.addConsultantIntoScheduleByID(id, date);
    }

    @Override
    public void addSupervisorIntoScheduleByName(String name, String date) {
        Preconditions.checkNotNull(date, "日期不能为空");
        Preconditions.checkNotNull(name, "姓名不能为空");
        Integer id = administratorRepo.getSupervisorIDByName(name);
        administratorRepo.addSupervisorIntoScheduleByID(id, date);
    }

    @Override
    public void deleteConsultantFromScheduleByName(String name, String date) {
        Preconditions.checkNotNull(date, "日期不能为空");
        Preconditions.checkNotNull(name, "姓名不能为空");
        Integer id = administratorRepo.getConsultantIDByName(name);
        administratorRepo.deleteConsultantFromScheduleByID(id, date);
    }

    @Override
    public void deleteSupervisorFromScheduleByName(String name, String date) {
        Preconditions.checkNotNull(date, "日期不能为空");
        Preconditions.checkNotNull(name, "姓名不能为空");
        Integer id = administratorRepo.getSupervisorIDByName(name);
        administratorRepo.deleteSupervisorFromScheduleByID(id, date);
    }

    @Override
    public void deleteSupervisorFromScheduleByNameAll(String name) {
        Preconditions.checkNotNull(name, "姓名不能为空");
        Integer id = administratorRepo.getSupervisorIDByName(name);
        administratorRepo.deleteSupervisorFromScheduleByIDAll(id);
    }

    @Override
    public void deleteConsultantFromScheduleByNameAll(String name) {
        Preconditions.checkNotNull(name, "姓名不能为空");
        Integer id = administratorRepo.getConsultantIDByName(name);
        administratorRepo.deleteConsultantFromScheduleByIDAll(id);
    }

    @Override
    public void bindConsultantAndSupervisor(int consultant_id, int supervisor_id) {
        Preconditions.checkNotNull(consultant_id, "咨询师id不能为空");
        Preconditions.checkNotNull(supervisor_id, "督导id不能为空");
        administratorRepo.bindConsultantAndSupervisor(consultant_id, supervisor_id);
    }

    @Override
    public ArrayList<String> getSupervisorInfoByName(String name) {
        Preconditions.checkNotNull(name, "姓名不能为空");
        Integer id = administratorRepo.getSupervisorIDByName(name);
        return administratorRepo.getSupervisorAllSchedule(id);
    }

    @Override
    public ArrayList<String> getConsultantInfoByName(String name) {
        Preconditions.checkNotNull(name, "姓名不能为空");
        Integer id = administratorRepo.getConsultantIDByName(name);
        return administratorRepo.getConsultantAllSchedule(id);
    }

    @Override
    public Integer getConsultantMessageID(Integer consultant_id, Integer client_id, String consult_date) {
        Preconditions.checkNotNull(consultant_id, "咨询师id不能为空");
        Preconditions.checkNotNull(client_id, "用户id不能为空");
        Preconditions.checkNotNull(consult_date, "咨询日期不能为空");
        return administratorRepo.getConsultantMessageID(consultant_id, client_id, consult_date);
    }

    @Override
    public HSSFWorkbook ExportRecordToExcel(ArrayList<ConsultantMessages> arrayList) {
        HSSFWorkbook workbook = new HSSFWorkbook();
        String[] excelHeader = {"咨询记录ID", "咨询时刻", "消息内容", "发送者ID", "接收者ID", "发送者身份"};
        HSSFSheet sheet = workbook.createSheet("咨询记录表");
        HSSFRow row = sheet.createRow(0);
        HSSFCellStyle style = workbook.createCellStyle();
        style.setAlignment(HorizontalAlignment.CENTER);
        for (int i = 0; i < excelHeader.length; i++) {
            HSSFCell cell = row.createCell(i);
            cell.setCellValue(excelHeader[i]);
            cell.setCellStyle(style);
            sheet.autoSizeColumn(i);
            sheet.setColumnWidth(cell.getColumnIndex(), 100 * 50);
        }

        for (int i = 0; i < arrayList.size(); i++) {
            row = sheet.createRow(i + 1);
            ConsultantMessages consultantMessages = arrayList.get(i);
            row.createCell(0).setCellValue(consultantMessages.getConsult_message_id());
            row.createCell(1).setCellValue(consultantMessages.getStart_time());
            row.createCell(2).setCellValue(consultantMessages.getContent());
            row.createCell(3).setCellValue(consultantMessages.getFromName());
            row.createCell(4).setCellValue(consultantMessages.getToName());
            if (consultantMessages.getTag() == 0) {
                row.createCell(5).setCellValue("咨询师");
            } else {
                row.createCell(5).setCellValue("用户");
            }
        }
        return workbook;
    }

    @Override
    public ArrayList<ConsultantMessages> getMessageContentByID(Integer consult_message_id) {
        return administratorRepo.getMessageContentByID(consult_message_id);
    }

    @Override
    public ArrayList<HelpMessage> getMessageHelpContentByID(Integer consult_message_id) {
        return administratorRepo.getMessageHelpContentByID(consult_message_id);
    }

    @Override
    public ArrayList<String> getBindRelationship(Integer consultant_id) {
        return administratorRepo.getBindRelationship(consultant_id);
    }

    @Override
    public void deleteConsultantFromMessageByIDAll(Integer consultant_id) {
        administratorRepo.deleteConsultantFromMessageByIDAll(consultant_id);
    }

    @Override
    public void deleteConsultantFromBindByIDAll(Integer consultant_id) {
        administratorRepo.deleteConsultantFromBindByIDAll(consultant_id);
    }

    @Override
    public void deleteSupervisorFromBindByIDAll(Integer supervisor_id) {
        administratorRepo.deleteSupervisorFromBindByIDAll(supervisor_id);
    }

    @Override
    public void addConsultMessage(ConsultInfo consultInfo) {
        consultInfoRepo.addConsultMessage(consultInfo);
    }

    @Override
    public ArrayList<ConsultInfo> getAllConsultMessage() {
        return consultInfoRepo.getAllConsultMessage();
    }

    @Override
    public ConsultInfo getConsultMessage(String name, String date) {
        return consultInfoRepo.getConsultMessage(name, date);
    }

    @Override
    public void addConsultMessageAll(ConsultantMessages consultantMessages) {
        consultantMessagesRepo.addConsultMessageAll(consultantMessages);
    }

    @Override
    public ArrayList<FigureData> getAllConsultNumberLimitK(String month, Integer k) {
        return newChatRepo.getAllConsultNumberLimitK(month, k);
    }

    @Override
    public ArrayList<FigureData2> gettodayConsultNumberLimitK(String month, Integer day, Integer k) {
        return newChatRepo.gettodayConsultNumberLimitK(month, day, k);
    }
}
